package com.bysj.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bysj.entity.Guide;
import com.bysj.fenye.pagevo.GuidePageVO;
import com.bysj.service.GuideService;
import com.bysj.service.impl.GuideServiceImpl;

public class GuideServlet extends HttpServlet {
	private GuidePageVO pagevo=new GuidePageVO(10);
	private GuideService gs=new GuideServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m=request.getParameter("m");
		String method=request.getParameter("method");
		if("pre".equals(method)){
			this.pagevo.setPrevious();
		}else if("next".equals(method)){
			this.pagevo.setNext();
		}else if("last".equals(method)){
			this.pagevo.setLast();
		}else if("index".equals(method)){
			String ind=request.getParameter("ind");
			this.pagevo.setIndex(Integer.parseInt(ind.trim()));
		}else{
			this.pagevo.setFirst();
		}
		if("AllGuide".equals(m)){
			request.getSession().setAttribute("glist", pagevo);
			request.getRequestDispatcher("../guide/allguide.jsp").forward(request, response);
		}else if("DelGuide".equals(m)){
			Guide g=new Guide();
			g.setId(Integer.parseInt(request.getParameter("id")));
			gs.DelGuide(g);
			request.getSession().setAttribute("glist", pagevo);
			request.getRequestDispatcher("../guide/allguide.jsp").forward(request, response);
		}else if("updateGuideDocById".equals(m)){
			request.getSession().setAttribute("updateGuideDocById", request.getParameter("id"));
			request.getRequestDispatcher("../guide/updateguidedoc.jsp").forward(request, response);
		}else if("DetailGuide".equals(m)){
			Guide g=new Guide();
			g.setId(Integer.parseInt(request.getParameter("id")));
			Guide guide=gs.queryGuide(g);
			request.getSession().setAttribute("guide",guide);
			request.getRequestDispatcher("../guide/detailGuide.jsp").forward(request, response);
		}else if("AddNewGuide".equals(m)){
			String title=request.getParameter("title");
			String time=request.getParameter("time");
			String content=request.getParameter("guideFCKeditor");
			String type=request.getParameter("type");
			Guide g=new Guide();
			g.setTitle(title);
			g.setTime(time);
			g.setType(type);
			g.setContent(content);
			gs.addGuide(g);
			request.getSession().setAttribute("glist", pagevo);
			request.getRequestDispatcher("../guide/allguide.jsp").forward(request, response);
		}
			
	}
}
